home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Windows 95 with MFC
/
Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso
/
NT
/
CODE
/
CHAP12
/
BITMAPDEMO
/
BITMAPDEMO.CPP
next >
Wrap
C/C++ Source or Header
|
1996-04-05
|
8KB
|
292 lines
//***********************************************************************
//
// BitmapDemo.cpp
//
//***********************************************************************
#include <afxwin.h>
#include "Resource.h"
#include "BitmapDemo.h"
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_CREATE ()
ON_WM_ERASEBKGND ()
ON_WM_PAINT ()
ON_WM_QUERYNEWPALETTE ()
ON_WM_PALETTECHANGED ()
ON_COMMAND (IDM_OPTIONS_DRAW_OPAQUE, OnOptionsDrawOpaque)
ON_COMMAND (IDM_OPTIONS_DRAW_TRANSPARENT, OnOptionsDrawTransparent)
ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
ON_UPDATE_COMMAND_UI (IDM_OPTIONS_DRAW_OPAQUE, OnUpdateDrawOpaqueUI)
ON_UPDATE_COMMAND_UI (IDM_OPTIONS_DRAW_TRANSPARENT,
OnUpdateDrawTransparentUI)
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
m_bDrawOpaque = TRUE;
Create (NULL, "Bitmap Demo", WS_OVERLAPPEDWINDOW, rectDefault,
NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
}
int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
{
if (CFrameWnd::OnCreate (lpcs) == -1)
return -1;
m_bitmap.LoadBitmap (IDR_BITMAP);
CClientDC dc (this);
if (dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) {
struct {
LOGPALETTE lp;
PALETTEENTRY ape[63];
} pal;
LOGPALETTE* pLP = (LOGPALETTE*) &pal;
pLP->palVersion = 0x300;
pLP->palNumEntries = 64;
for (int i=0; i<64; i++) {
pLP->palPalEntry[i].peRed = 0;
pLP->palPalEntry[i].peGreen = 0;
pLP->palPalEntry[i].peBlue = 255 - (i * 4);
pLP->palPalEntry[i].peFlags = 0;
}
m_palette.CreatePalette (pLP);
}
return 0;
}
BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
{
CRect rect;
GetClientRect (&rect);
CPalette* pOldPalette;
if ((HPALETTE) m_palette != NULL) {
pOldPalette = pDC->SelectPalette (&m_palette, FALSE);
pDC->RealizePalette ();
}
DoGradientFill (pDC, &rect);
if ((HPALETTE) m_palette != NULL)
pDC->SelectPalette (pOldPalette, FALSE);
return TRUE;
}
void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);
CPaintDC dc (this);
BITMAP bm;
m_bitmap.GetObject (sizeof (BITMAP), &bm);
int cx = (rect.Width () / (bm.bmWidth + 8)) + 1;
int cy = (rect.Height () / (bm.bmHeight + 8)) + 1;
int i, j, x, y;
for (i=0; i<cx; i++) {
for (j=0; j<cy; j++) {
x = 8 + (i * (bm.bmWidth + 8));
y = 8 + (j * (bm.bmHeight + 8));
if (m_bDrawOpaque)
m_bitmap.Draw (&dc, x, y);
else
m_bitmap.DrawTransparent (&dc, x, y, RGB (255, 0, 0));
}
}
}
BOOL CMainWindow::OnQueryNewPalette ()
{
if ((HPALETTE) m_palette == NULL) // Shouldn't happen, but
return 0; // let's be sure
CClientDC dc (this);
CPalette* pOldPalette = dc.SelectPalette (&m_palette, FALSE);
UINT nCount;
if (nCount = dc.RealizePalette ())
Invalidate ();
dc.SelectPalette (pOldPalette, FALSE);
return nCount;
}
void CMainWindow::OnPaletteChanged (CWnd* pFocusWnd)
{
if ((HPALETTE) m_palette == NULL) // Shouldn't happen, but
return; // let's be sure
if (pFocusWnd != this) {
CClientDC dc (this);
CPalette* pOldPalette = dc.SelectPalette (&m_palette, FALSE);
if (dc.RealizePalette ())
Invalidate ();
dc.SelectPalette (pOldPalette, FALSE);
}
}
void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
{
CBrush* pBrush[64];
for (int i=0; i<64; i++)
pBrush[i] = new CBrush (PALETTERGB (0, 0, 255 - (i * 4)));
int nWidth = pRect->Width ();
int nHeight = pRect->Height ();
CRect rect;
for (i=0; i<nHeight; i++) {
rect.SetRect (0, i, nWidth, i + 1);
pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
}
for (i=0; i<64; i++)
delete pBrush[i];
}
void CMainWindow::OnOptionsDrawOpaque ()
{
m_bDrawOpaque = TRUE;
Invalidate ();
}
void CMainWindow::OnOptionsDrawTransparent ()
{
m_bDrawOpaque = FALSE;
Invalidate ();
}
void CMainWindow::OnOptionsExit ()
{
SendMessage (WM_CLOSE, 0, 0);
}
void CMainWindow::OnUpdateDrawOpaqueUI (CCmdUI* pCmdUI)
{
pCmdUI->SetCheck (m_bDrawOpaque ? 1 : 0);
}
void CMainWindow::OnUpdateDrawTransparentUI (CCmdUI* pCmdUI)
{
pCmdUI->SetCheck (m_bDrawOpaque ? 0 : 1);
}
/////////////////////////////////////////////////////////////////////////
// CMaskedBitmap member functions
void CMaskedBitmap::Draw (CDC* pDC, int x, int y)
{
BITMAP bm;
GetObject (sizeof (BITMAP), &bm);
CPoint size (bm.bmWidth, bm.bmHeight);
pDC->DPtoLP (&size);
CPoint org (0, 0);
pDC->DPtoLP (&org);
CDC dcMem;
dcMem.CreateCompatibleDC (pDC);
CBitmap* pOldBitmap = dcMem.SelectObject (this);
dcMem.SetMapMode (pDC->GetMapMode ());
pDC->BitBlt (x, y, size.x, size.y, &dcMem, org.x, org.y, SRCCOPY);
dcMem.SelectObject (pOldBitmap);
}
void CMaskedBitmap::DrawTransparent (CDC* pDC, int x, int y,
COLORREF crColor)
{
BITMAP bm;
GetObject (sizeof (BITMAP), &bm);
CPoint size (bm.bmWidth, bm.bmHeight);
pDC->DPtoLP (&size);
CPoint org (0, 0);
pDC->DPtoLP (&org);
// Create a memory DC (dcImage) and select the bitmap into it
CDC dcImage;
dcImage.CreateCompatibleDC (pDC);
CBitmap* pOldBitmapImage = dcImage.SelectObject (this);
dcImage.SetMapMode (pDC->GetMapMode ());
// Create a second memory DC (dcAnd) and in it create an AND mask
CDC dcAnd;
dcAnd.CreateCompatibleDC (pDC);
dcAnd.SetMapMode (pDC->GetMapMode ());
CBitmap bitmapAnd;
bitmapAnd.CreateBitmap (bm.bmWidth, bm.bmHeight, 1, 1, NULL);
CBitmap* pOldBitmapAnd = dcAnd.SelectObject (&bitmapAnd);
dcImage.SetBkColor (crColor);
dcAnd.BitBlt (org.x, org.y, size.x, size.y, &dcImage, org.x, org.y,
SRCCOPY);
// Create a third memory DC (dcXor) and in it create an XOR mask
CDC dcXor;
dcXor.CreateCompatibleDC (pDC);
dcXor.SetMapMode (pDC->GetMapMode ());
CBitmap bitmapXor;
bitmapXor.CreateCompatibleBitmap (&dcImage, bm.bmWidth, bm.bmHeight);
CBitmap* pOldBitmapXor = dcXor.SelectObject (&bitmapXor);
dcXor.BitBlt (org.x, org.y, size.x, size.y, &dcImage, org.x, org.y,
SRCCOPY);
dcXor.BitBlt (org.x, org.y, size.x, size.y, &dcAnd, org.x, org.y,
0x220326);
// Copy the pixels in the destination rectangle to a temporary
// memory DC (dcTemp)
CDC dcTemp;
dcTemp.CreateCompatibleDC (pDC);
dcTemp.SetMapMode (pDC->GetMapMode ());
CBitmap bitmapTemp;
bitmapTemp.CreateCompatibleBitmap (&dcImage, bm.bmWidth, bm.bmHeight);
CBitmap* pOldBitmapTemp = dcTemp.SelectObject (&bitmapTemp);
dcTemp.BitBlt (org.x, org.y, size.x, size.y, pDC, x, y, SRCCOPY);
// Generate the final image by applying the AND and XOR masks to
// the image in the temporary memory DC
dcTemp.BitBlt (org.x, org.y, size.x, size.y, &dcAnd, org.x, org.y,
SRCAND);
dcTemp.BitBlt (org.x, org.y, size.x, size.y, &dcXor, org.x, org.y,
SRCINVERT);
// Blit the resulting image to the screen
pDC->BitBlt (x, y, size.x, size.y, &dcTemp, org.x, org.y, SRCCOPY);
// Restore the default bitmaps
dcTemp.SelectObject (pOldBitmapTemp);
dcXor.SelectObject (pOldBitmapXor);
dcAnd.SelectObject (pOldBitmapAnd);
dcImage.SelectObject (pOldBitmapImage);
}